/**
 * @file    yuyv_camera.hpp
 * @brief   yuyv功能类摄像头初始化
 * @author  Haozhong Xu
 * @date    2025-05-27
 * @version 1.0
 * 
 * @note    初始化摄像头输出yuyv流
 */
#ifndef YUYV_CAMERA_HPP
#define YUYV_CAMERA_HPP

#include <iostream>
#include <string>
#include <vector>
#include "camera/camera_base.hpp"

/**
 * @class YUYVCamera
 * @brief YUYVCamera 摄像头功能类
 *
 * 构造函数初始化摄像头输出yuyv数据，
 * UpdateFrame()从内核缓冲区中提取一帧
 */
class YUYVCamera : public CameraBase {
public:
    YUYVCamera(const std::string &devicePath, int width, int height, bool autoInit = true)
        : CameraBase(devicePath, width, height) 
    {
        if (autoInit) {
            try {
                Init();
            } catch (const std::exception &e) {
                std::cerr << "程序异常终止" << e.what() <<std::endl;
            }

        }
    }
     
    ~YUYVCamera() override;

    void Init() override;
    void Close() override;

    // 更新一帧到shared buffer中
    bool UpdateFrame();
    const std::vector<uint8_t> &GetFrame() const;

    // 保存一帧使用
    bool CaptureOneYUYVFrame(const std::string &filename);


private:
    bool InitDevice();
    bool InitFormat();
    bool InitMmap();
    bool StartStreaming();
    bool StopStreaming();

private:
    void *_mmapPtr[4] {};
    size_t _mmapSize[4] {};
    bool _streaming = false;
    std::vector<uint8_t> _yuyvData;

};


#endif /* MJPEG_CAMERA_HPP */